﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports.Engine;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;


namespace Recon.Portal.WebUI
{
    public partial class Reports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //fetch data based on report search criteria
            ReportSearchCriteria criteria = (ReportSearchCriteria)Session["ReportSearchCriteria"];
            Lists lstReportData = new Lists();
            DataTable reportData = lstReportData.GetReportData(criteria);

            //prepare report with data
            ReportDocument rptDoc = new ReportDocument();
            if (criteria.ReportType == ReportSearchCriteria.Type.Invoice)
            {
                rptDoc.Load(Server.MapPath("ReportInvoice.rpt"));
            }
            else if (criteria.ReportType == ReportSearchCriteria.Type.Statement)
            {
                rptDoc.Load(Server.MapPath("ReportStatement.rpt"));
            }
            rptDoc.SetDataSource(reportData);
            
            //apply report title/subject
             string criteriaReport = string.Empty;
            criteriaReport = criteria.Title;
            if (criteriaReport.ToUpper().StartsWith("I"))
            {
                criteria.Title = "Invoice Report";
            }
            else if (criteriaReport.ToUpper().StartsWith("S"))
            {
                criteria.Title = "Statement Report";
            }
            
            rptDoc.SummaryInfo.ReportTitle = criteria.Title;
            if (criteria.FromDate != DateTime.MinValue && criteria.ToDate != DateTime.MaxValue)
            {
                rptDoc.SummaryInfo.ReportComments = "For the period " + criteria.FromDate.ToString("dd/MM/yyyy") + " to " + criteria.ToDate.ToString("dd/MM/yyyy");
            }
            else if(criteria.FromDate != DateTime.MinValue)
            {
                rptDoc.SummaryInfo.ReportComments = "Since " + criteria.FromDate.ToString("dd/MM/yyyy");
            }
            else if(criteria.ToDate != DateTime.MaxValue)
            {
                rptDoc.SummaryInfo.ReportComments = "Upto " + criteria.ToDate.ToString("dd/MM/yyyy");
            }
            else
            {
                rptDoc.SummaryInfo.ReportComments = "All Dates";
            }

            //apply correct column headers
            int fieldCount = rptDoc.ReportDefinition.Sections[1].ReportObjects.Count;
            if (criteria.FieldList.Count > 0)
            {
                foreach (KeyValuePair<string, string> field in criteria.FieldList)
                {
                    for (int ctr = 0; ctr <= fieldCount - 1; ctr++)
                    {
                        if (rptDoc.ReportDefinition.Sections[1].ReportObjects[ctr].GetType() == typeof(FieldHeadingObject) && field.Key == ((FieldHeadingObject)rptDoc.ReportDefinition.Sections[1].ReportObjects[ctr]).Text)
                        {
                            ((FieldHeadingObject)rptDoc.ReportDefinition.Sections[1].ReportObjects[ctr]).Text = field.Value;
                            break;
                        }
                    }
                }
            }
            else
            {
                if (criteria.ReportType.ToString() == ReportSearchCriteria.Type.Invoice.ToString())
                {
                    for (int i = 0; i <= fieldCount - 1; i++)
                    {
                        if (rptDoc.ReportDefinition.Sections[1].ReportObjects[i].GetType() == typeof(FieldHeadingObject))
                        {
                            ((FieldHeadingObject)rptDoc.ReportDefinition.Sections[1].ReportObjects[i]).Text = "Column"+i;
                            break;
                        }
                    }
                }
                else if (criteria.ReportType.ToString() == ReportSearchCriteria.Type.Statement.ToString())
                {
                    for (int i = 0; i <= fieldCount - 1; i++)
                    {
                        if (rptDoc.ReportDefinition.Sections[1].ReportObjects[i].GetType() == typeof(FieldHeadingObject))
                        {
                            ((FieldHeadingObject)rptDoc.ReportDefinition.Sections[1].ReportObjects[i]).Text = "Column" + i;
                            break;
                        }
                    }
                }

            }

            //display report on report viewer
            crReconReport.ReportSource = rptDoc;
            crReconReport.DataBind();
        }
    }
}